package com.baomidou.springwind.test.Proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Administrator on 2017-02-04.
 */

class DynamicProxyHandler implements InvocationHandler {

    private Object object;

    public DynamicProxyHandler(Object object) {
        this.object = object;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("proxy.class = " + proxy.getClass());

        System.out.println("method = " + method);

        System.out.println("args = " + args);
        if (args != null) {
            for (Object o : args) {
                System.out.println("  " + o);
            }
        }

        return method.invoke(object,args);
    }
}

public class SimpleDynamicProxy {
    public static void consumer(Interface anInterface) {
        anInterface.doSomething();
        anInterface.doElseThing("  yhx");
    }

    public static void main(String[] args) {
        ReadObject readObject = new ReadObject();
        consumer(readObject);
        Interface proxy = (Interface) Proxy.newProxyInstance(Interface.class.getClassLoader(), new Class[]{Interface.class}
        ,new DynamicProxyHandler(readObject));
        consumer(proxy);
    }

}
